﻿using System;
using System.Collections.Generic;
using System.Data;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain;
using Ue.Qcdz.Domain.Products;

namespace Ue.Qcdz.Repositories.Products
{
    public class ProductRepository : SqlServerRepository<Product>, IProductRepository
    {
        #region Overrides of SqlServerRepositoryBase<Product>

        protected override string GetDataTableName()
        {
            return "product";
        }

        protected override string[] GetBaseInsertFields()
        {
            return new string[]
                       {
                           ProductFactory.FieldNames.EntityKey,
                           ProductFactory.FieldNames.ProductImageUrl,
                           ProductFactory.FieldNames.ProductLink,
                           ProductFactory.FieldNames.ProductState,
                           ProductFactory.FieldNames.ProductTitle
                      };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[]
                       {
                           ProductFactory.FieldNames.ProductImageUrl,
                           ProductFactory.FieldNames.ProductLink,
                           ProductFactory.FieldNames.ProductState,
                           ProductFactory.FieldNames.ProductTitle
                      };
        }

        protected override IDataParameter[] BuildParameters(Product entity)
        {
            return new IDataParameter[]
                       {
                           RepositoryDriver.BuildParameter(ProductFactory.FieldNames.EntityKey,entity.Key),
                           RepositoryDriver.BuildParameter(ProductFactory.FieldNames.ProductImageUrl,entity.ProductImageUrl),
                           RepositoryDriver.BuildParameter(ProductFactory.FieldNames.ProductLink,entity.ProductLink),
                           RepositoryDriver.BuildParameter(ProductFactory.FieldNames.ProductState,entity.State),
                           RepositoryDriver.BuildParameter(ProductFactory.FieldNames.ProductTitle,entity.ProductTitle)
                       };
        }

        protected override IWhereClause BuildWhereClauseBy(IDataQuery dataQuery)
        {
            return null;
        }

        protected override IDictionary<string, ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            return null;
        }

        #endregion

        #region Implementation of IProductRepository

        public IList<Product> FindBy(int num, EntityState sate)
        {
            var query = string.Format("SELECT TOP {0} * FROM product WHERE state=@state", num);
            return this.BuildEntitiesFromSql(query, this.RepositoryDriver.BuildParameter("@state", sate));
        }

        #endregion
    }
}
